package com.xiaoming.service;

import com.xaioming.service.CodeService;
import com.xiaoming.dao.CodeRepository;
import com.xiaoming.entity.Code;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class CodeServiceImpl  implements CodeService {
    @Autowired
    private CodeRepository repository;

    @Override
    public int validate(String email, String validator) throws Exception {
        List<Code> codes = repository.findByEmailAndCodeOrderByExpiretimeDesc(email, validator);
        if (codes.size() == 0 || !validator.equals(codes.get(0).getCode())) {
            return 1;
        }
        Code code = codes.get(0);
        long current = System.currentTimeMillis();
        if (code.getExpiretime().getTime() < current) {
            return 2;
        }
        return 0;
    }
}
